﻿using System;
using System.Collections.Generic;
using System.Text;

public class Dungeon
{
    public int Size { get; private set; }

    public int MinPartitionDimensions { get; private set; }
    public float MinSplitRatio { get; private set; }
    public float MaxSplitRatio { get; private set; }
    public int MinRoomMargin { get; private set; }
    public float MinRoomDimensions { get; private set; }
    public float MaxRoomDimensions { get; private set; }
    public DungeonPartition RootNode { get; private set; }

    public List<Room> Rooms { get; private set; }
    public List<Corridor> Corridors { get; private set; }

    public Dungeon(int size, int minPartitionDimensions, float minSplitRatio, float maxSplitRatio, int minRoomMargin, float minRoomDimensions, float maxRoomDimensions)
    {
        this.Size = size;
        this.MinPartitionDimensions = minPartitionDimensions;
        this.MinRoomMargin = minRoomMargin;
        this.MinRoomDimensions = minRoomDimensions;
        this.MaxRoomDimensions = maxRoomDimensions;
        this.MinSplitRatio = minSplitRatio;
        this.MaxSplitRatio = maxSplitRatio;
        RootNode = new DungeonPartition(null, this);
    }

    public Dungeon(int size)
    {
        this.Size = size;
        this.MinPartitionDimensions = 16;
        RootNode = new DungeonPartition(null, this);
    }

    public int SplitAll()
    {
        return RootNode.Partition();
    }

    public List<DungeonPartition> GetLatestPartitions()
    {
        List<DungeonPartition> partitions = new List<DungeonPartition>();
        RootNode.AppendWhenChildrenAreLeaves(partitions);
        return partitions;
    }

    public List<DungeonPartition> GetAllButLatestPartitions()
    {
        List<DungeonPartition> partitions = new List<DungeonPartition>();
        RootNode.AppendWhenChildrenAreNotLeaves(partitions);
        return partitions;
    }

    public virtual List<Room> GenerateRooms()
    {
        UnityEngine.MonoBehaviour.print("generating rooms in Dungeon");
        Rooms = new List<Room>();
        RootNode.GenerateRooms(Rooms);
        UnityEngine.MonoBehaviour.print("Rooms created in Dungeon: " + Rooms.Count.ToString());
        return Rooms;
    }

    public virtual List<Corridor> GenerateCorridors()
    {
        Corridors = new List<Corridor>();
        CorridorConnectionManager connector = new CorridorConnectionManager(RootNode);
        RootNode.GetCorridors(Corridors);

        return Corridors;
    }

    public int AbsoluteMinRoomDimension { get { return this.MinPartitionDimensions - this.MinRoomMargin * 2; } }

}